import multiprocessing

import requests
import json
from fake_useragent import UserAgent
from proxy_helper import ProxyHelperPost
import threading


ua = UserAgent().random


count = 0
headers = {
    "referer": "https://www.gaokao.cn/",
    "user-agent": ua,
}
url = "https://api.zjzw.cn/web/api/"
page = 0
while True:
    page += 1
    params = {
        "city_id": "",
        "keyword": "",
        "nature": "",
        "page": str(page),
        "province_id": "",
        "request_type": "1",
        "school_tese": "",
        "school_type": "",
        "sort": "ruanke_rank",
        "sorttype": "asc",
        "type": "",
        "size": "20",
        "uri": "apidata/api/gkv3/school/lists",
        # "signsafe": "e88969610fe718af40af55a474c109d6"
    }

    response = requests.post(url, headers=headers, params=params)
    res = response.json()
    print(res)
    school_list = res['data']['item']
    if len(school_list) < 20:
        break
    for school in school_list:
        school_name = school['name']
        school_id = school['school_id']
        school_detail_url = 'https://www.gaokao.cn/school/'+str(school_id)+'/provinceline'
        school_url = "https://api.zjzw.cn/web/api/"
        school_params = {
            "local_batch_id": "7",
            "local_province_id": "41",
            "local_type_id": "1",
            "page": "1",
            "school_id": str(school_id),
            "size": "10",
            "sp_xuanke": "",
            "special_group": "",
            "uri": "apidata/api/gk/score/special",
            "year": "2023",
            # "signsafe": "200eea179aee40622fe35bd1383b593b"
        }
        response = requests.post(school_url, headers=headers, params=school_params)
        count += 1
        print(f"第{count}所",school_name, school_id,school_detail_url,response.json())
        print(response)
        with open('school.json', 'a', encoding='utf-8') as f:
            f.write(f"第{count}所{school_name,str(school_id),school_detail_url,json.dumps(response.json(), ensure_ascii=False)}"+'\n')

